$(function(){
	$('#receive_time , #exchange_time').daterangepicker({
		autoUpdateInput:false,
		language:  'zh-CN',
	    timePicker: true,
	    timePickerIncrement: 30,
	    format: 'YYYY-MM-DD',
	    locale: {
	        format: 'YYYY-MM-DD',
	        daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
	        monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
	    }
	});
});

function getCouponReport(){
	$('#table_list').DataTable({
	    dom: '<"top">rt<"bottom"flip><"clear">',
	    language: {
	        "sUrl":'../static/plugins/datatables/language.json'
	    },
	    "searching": false,
	    "processing": true,
	    "serverSide": true,
	    ajax:{
	        url:'/coupon/coupon-report',
	        type:'post',
	        data:function(d){
	        	d.type = $('#type').val();
	        	d.coupon_id = $('#coupon_id').val();
	        	d.title = $('#title').val();
	        	d.is_exchange = $('#is_exchange').val();
	        	d.receive_time = $('#receive_time').val();
	        	d.exchange_time = $('#exchange_time').val();
	        },
	        error:getError
	    },
	    aoColumns: [
	        { "data": "id", "bSortable": false},
	        { "data": "type", "bSortable": false},
	        { "data": "coupon_id", "bSortable": false},
	        { "data": "title", "bSortable": false },
	        { "data": "mobile", "bSortable": false},
	        { "data": "receive_time", "bSortable": false},
	        { "data": "exchange_time", "bSortable": false},
	        { "data": "use_mark", "bSortable": false},
	        { "data": "is_exchange", "bSortable": false},
	    ],
	    columnDefs: [
	    	{
                "targets": [1],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return coupon_type[data];
                }
            },
            {
                "targets": [7],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return use_mark[data];
                }
            },
            {
                "targets": [8],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                	return exchange_status[data];
                }
            },
	    ]
	});
}

function table_list_reload(){
	if($('#search_count').val() == 0){
		$('#search_count').val(1);
		getCouponReport();
	}else{
		var table = $('#table_list').DataTable();
	    table.ajax.reload();
	}
}

$('#reset_button').click(function(){
	$('#type').val('');
	$('#coupon_id').val('');
	$('#title').val('');
	$('#is_exchange').val('');
	$('#receive_time').val('');
	$('#exchange_time').val('');
    table_list_reload();
});

function exportCouponReport(){
	var form = $('#coupon-report-select');
	form.submit();
}